package com.abe.mvp.view.fragment;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.abe.config.SettingParam;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.view.activity.FragmentCallbackListener.SettingCallbackListener;
import com.abe.mvp.view.adapter.SettingMenuAdapter;
import com.abe.mvp.view.fragment.base.BaseSettingFragment;
import com.abe.strengthtrainer.R;

public class SettingLoginTypeFragment extends BaseSettingFragment implements
		OnItemClickListener {

	@SuppressWarnings("unused")
	private SettingCallbackListener callback;

	private GridView setting_language_grid_menu;
	private List<MenuBean> menuBeans;
	private SettingMenuAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		callback = (SettingCallbackListener) getActivity();
		View view = this.onCreateView(inflater,
				R.layout.activity_set_fragment_login_type, container,
				savedInstanceState);
		initView(view);
		return view;
	}

	public void initView(View view) {
		setting_language_grid_menu = (GridView) view
				.findViewById(R.id.setting_language_grid_menu);
		SettingBean bean = SettingParam.getSettingBean();
		boolean type_scan = true, type_swingcard = true;
		switch (bean.getLoginType()) {
		case SettingParam.loginType_no:
			type_scan = false;
			type_swingcard = false;
			break;
		case SettingParam.loginType_scan:
			type_swingcard = false;
			break;
		case SettingParam.loginType_swingcard:
			type_scan = false;
			break;
		}
		menuBeans = SettingParam.getLoginTypeMenuBeans(type_scan,
				type_swingcard);
		adapter = new SettingMenuAdapter(getActivity(), menuBeans, scale,
				SettingMenuAdapter.TYPE_LOGINTYPE);
		setting_language_grid_menu.setAdapter(adapter);
		setting_language_grid_menu.setOnItemClickListener(this);
	}

	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
		/** 刷新显示 */
		MenuBean bean = menuBeans.get(pos);
		bean.setSelected(!bean.isSelected());
		menuBeans.set(pos, bean);
		adapter.setListData(menuBeans);
		setting_language_grid_menu.setAdapter(adapter);
		/** 保存修改 */
		saveChange();
	}

	@Override
	protected void saveChange() {
		SettingBean settingBean = SettingParam.getSettingBean();
		MenuBean bean_scan = menuBeans.get(0);
		MenuBean bean_swing = menuBeans.get(1);
		if (bean_scan.isSelected() && bean_swing.isSelected()) {
			settingBean.setLoginType(SettingParam.loginType_all);
		} else if (bean_scan.isSelected() && !bean_swing.isSelected()) {
			settingBean.setLoginType(SettingParam.loginType_scan);
		} else if (!bean_scan.isSelected() && bean_swing.isSelected()) {
			settingBean.setLoginType(SettingParam.loginType_swingcard);
		} else {
			settingBean.setLoginType(SettingParam.loginType_no);
		}
		SettingParam.updateSettingBean(settingBean);
	}
}
